import { createRouter, createWebHashHistory } from 'vue-router'
import Index from '../views/index.vue'
import encrypt from '@/utils/encrypt'
import i18n from '@/i18n'
import { fa } from 'element-plus/es/locales.mjs'
import { ElMessage } from 'element-plus'
const { t } = i18n.global
const routes = [
    {
        path: '/',
        name: 'Index',
        component: Index,
        redirect: '/home',
        children: [
            {
                path: 'home',
                name: 'Home',
                component: () => import('@/views/Home/index.vue'),
                meta: {
                    menu: false,
                    back: false,
                    title: t('Header.Home')
                }
            },
            {
                path: 'interactive-erase',
                name: 'InteractiveErase',
                component: () => import('@/views/InteractiveErase/index.vue'),
                meta: {
                    menu: false,
                    back: false,
                    title: '交互式擦除'
                }
            },
            {
                path: 'batchupload',
                name: 'BatchUpload',
                component: () => import('@/views/BatchUpload/index.vue'),
                meta: {
                    menu: false,
                    back: false,
                    title: t('Header.BatchProcess')
                }
            },
            {
                path: 'upload',
                name: 'Upload',
                component: () => import('@/views/Upload/index.vue'),
                meta: {
                    menu: false,
                    back: false,
                    title: t('Header.BatchProcess')
                }
            },
            {
                path: 'price',
                name: 'Price',
                component: () => import('@/views/Price/index.vue'),
                meta: {
                    menu: false,
                    back: false,
                    title: t('Header.Price')
                }
            },
            {
                path: 'account',
                name: 'Account',
                component: () => import('@/views/Account/index.vue'),
                redirect: '/account/profile',
                children: [
                    {
                        path: 'profile',
                        name: 'Profile',
                        component: () => import('@/views/Account/Profile.vue'),
                        meta: {
                            title: t('Header.Account')
                        }
                    },
                    {
                        path: 'subscription',
                        name: 'Subscription',
                        component: () => import('@/views/Account/Subscription.vue'),
                        meta: {
                            title: t('Header.Subscription')
                        }
                    }
                ]
            }
        ]
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('@/views/Login/index.vue'),
        meta: {
            menu: false,
            back: false,
            title: t('Header.Login')
        }
    },
    {
        path: '/register',
        name: 'Register',
        component: () => import('@/views/Register/index.vue'),
        meta: {
            menu: false,
            back: false,
            title: t('Header.Register')
        }
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})

// 不需要验证token的白名单路由
const whiteList = ['/home', '/login', '/register', '/price']

router.beforeEach((to, from, next) => {
    const token = encrypt.GetLocalData('accessToken')

    // 在白名单中的路由直接放行
    if (whiteList.includes(to.path)) {
        next()
        return
    }

    // 其他路由需要验证token
    if (token) {
        next()
    } else {
        // 没有token则跳转到登录页面
        next('/login')
    }
})
export default router
